#include "vl_file.h"

/**
 * +======================================================================================================================+
 * @Author: CimZzz
 * @Date: 2022-02-11 16:02:17
 * 
 * 代码分割块，以下内容涉及: 
 * 
 * 宏定义区分系统特性
 * 
 */
#if defined _WIN32 || defined _WIN64
	// Windows 系统
	#define FILE_SEPERATOR '\\' // 文件分割符
	#define FILE_PATH_CONVERTOR '\\'
	#include <Windows.h>
	#include <io.h>
	#include <sys/stat.h>
	#include <sys/types.h>
	#include <direct.h>
	#define stat _stat
	#define VL_MAX_PATH 2048
#elif defined __APPLE__ || defined __MACH__
	// Mac 系统
	#define FILE_SEPERATOR '/' // 文件分割符
	#define FILE_PATH_CONVERTOR '\\'
	#include <sys/stat.h>
	#include <dirent.h>
	#include <unistd.h>
	#include <libgen.h>
	#define VL_MAX_PATH PATH_MAX
#elif defined __linux__
	// Linux 操作系统
	#define FILE_SEPERATOR '/' // 文件分割符
	#define FILE_PATH_CONVERTOR '\\'
	#include <sys/stat.h>
	#include <dirent.h>
	#include <unistd.h>
	#include <cstring>
	#define VL_MAX_PATH PATH_MAX
#elif defined __unix || defined __unix__
	// Unix 操作系统
	#define FILE_SEPERATOR '/' // 文件分割符
	#define FILE_PATH_CONVERTOR '\\'
	#include <sys/stat.h>
	#include <dirent.h>
	#include <unistd.h>
	#include <cstring>
	#define VL_MAX_PATH PATH_MAX
#else
	#error Not support operator system!
#endif

using namespace VL;


namespace VL {


/**
 * @Author: CimZzz
 * @Date: 2023-02-28 18:01:48
 * 
 * 检查路径是否从根开始
 * 
 * @param path 路径
 * 
 * @return 如果是返回 true
 */
static bool _checkPathBeginRoot(const std::string& path) {
	#if defined _WIN32 || defined _WIN64
		return path.length() > 1 && path.at(1) == ':';
	#elif defined __APPLE__ || defined __MACH__
		return path.length() > 0 && path.at(0) == FILE_SEPERATOR;
	#elif defined __linux__
		return path.length() > 0 && path.at(0) == FILE_SEPERATOR;
	#elif defined __unix || defined __unix__
		return path.length() > 0 && path.at(0) == FILE_SEPERATOR;
	#else
		#error Not support operator system!
	#endif
}

/**
 * @Author: CimZzz
 * @Date: 2023-03-01 10:58:00
 * 
 * 检查路径是否为根路径
 * 
 * @param path 路径
 * 
 * @return 如果是根路径返回 true
 */
static bool _checkPathIsRoot(const std::string& path) {
	#if defined _WIN32 || defined _WIN64
		auto index = path.find_last_not_of(FILE_SEPERATOR);
		if(index == -1 || path.at(index) == ':') {
			return true;
		}
		return false;
	#elif defined __APPLE__ || defined __MACH__
		return path.length() == 1 && path.at(0) == FILE_SEPERATOR;
	#elif defined __linux__
		return path.length() == 1 && path.at(0) == FILE_SEPERATOR;
	#elif defined __unix || defined __unix__
		return path.length() == 1 && path.at(0) == FILE_SEPERATOR;
	#else
		#error Not support operator system!
	#endif
}

/**
 * @Author: CimZzz
 * @Date: 2023-02-28 18:36:51
 * 
 * 检查路径是否以分隔符开始
 * 
 * @param path 路径
 * 
 * @return 如果是返回 true
 */
static bool _checkStartWithSeperator(const std::string& path) {
	size_t pathLength = path.length();
	return pathLength > 0 && path.at(0) == FILE_SEPERATOR;
}

/**
 * @Author: CimZzz
 * @Date: 2023-02-28 18:01:48
 * 
 * 检查路径是否以分隔符结尾
 * 
 * @param path 路径
 * 
 * @return 如果是返回 true
 */
static bool _checkEndWithSeperator(const std::string& path) {
	size_t pathLength = path.length();
	return pathLength > 0 && path.at(pathLength - 1) == FILE_SEPERATOR && (pathLength == 1 || path.at(pathLength - 2) != FILE_PATH_CONVERTOR);
}

}


/**
 * @Author: CimZzz
 * @Date: 2023-02-28 16:07:55
 * 
 * 构造函数
 * 
 * @param path 路径
 */
File::File(const std::string& path): 
	absolutePath(""), name(""), ext(""), baseName(""), parentPath("") {
	this->_initFilePath(path.length() == 0 ? "." : path, false);
}

/**
 * @Author: CimZzz
 * @Date: 2023-02-28 16:09:52
 * 
 * 构造函数
 * 
 * @param dirPath 文件夹路径
 * @param relativePath 相对路径
 */
File::File(const std::string& dirPath, const std::string& relativePath): 
	absolutePath(""), name(""), ext(""), baseName(""), parentPath("") {
	std::string composePath;
	std::string processedRelativePath;
	if(_checkEndWithSeperator(relativePath)) {
		processedRelativePath = relativePath.substr(0, relativePath.length() - 1);
	} else {
		processedRelativePath = relativePath;
	}
	if(_checkEndWithSeperator(dirPath)) {
		if(_checkStartWithSeperator(processedRelativePath)) {
			composePath = dirPath + FILE_SEPERATOR + processedRelativePath;
		} else {
			composePath = dirPath + processedRelativePath;
		}
	} else {
		if(_checkStartWithSeperator(processedRelativePath)) {
			composePath = dirPath + processedRelativePath;
		} else {
			composePath = dirPath + FILE_SEPERATOR + processedRelativePath;
		}
	}
	this->_initFilePath(composePath, false);
}

/**
 * @Author: CimZzz
 * @Date: 2023-03-07 16:29:27
 * 
 * 构造函数
 * 
 * @param dirFile 父文件夹
 * @param childPath 子路径
 */
File::File(const File& dirFile, const std::string& childPath): File(dirFile.getAbsolutePath(), childPath) { }

/**
 * @Author: CimZzz
 * @Date: 2023-03-07 16:32:18
 * 
 * 构造函数
 * 
 * @param dirFile 父文件夹指针
 * @param childPath 子路径
 */
File::File(const File* dirFile, const std::string& childPath): File(dirFile->getAbsolutePath(), childPath) { }

/**
 * @Author: CimZzz
 * @Date: 2023-02-28 17:59:20
 * 
 * 私有构造函数
 */
File::File(): 
	absolutePath(""), name(""), ext(""), baseName(""), parentPath("") {
}


/**
 * @Author: CimZzz
 * @Date: 2023-02-28 18:25:20
 * 
 * 获取文件全路径
 * 
 * @return 文件全路径
 */
const std::string& File::getAbsolutePath() const {
	return this->absolutePath;
}

/**
 * @Author: CimZzz
 * @Date: 2023-02-28 18:56:33
 * 
 * 获取文件名
 * 
 * @return 文件名
 */
const std::string& File::getName() const {
	return this->name;
}

/**
 * @Author: CimZzz
 * @Date: 2023-02-28 18:56:53
 * 
 * 获取文件扩展名
 * 
 * @return 文件扩展名
 */
const std::string& File::getExt() const {
	return this->ext;
}

/**
 * @Author: CimZzz
 * @Date: 2023-02-28 18:57:46
 * 
 * 获取无扩展名文件名
 * 
 * @return 无扩展名文件名
 */
const std::string& File::getBaseName() const {
	return this->baseName;
}

/**
 * @Author: CimZzz
 * @Date: 2023-03-01 11:04:17
 * 
 * 获取父文件夹路径
 * 
 * @return 父文件夹路径
 */
const std::string& File::getParentPath() const {
	return this->parentPath;
}

/**
 * @Author: CimZzz
 * @Date: 2023-02-28 19:09:17
 * 
 * 判断文件是否存在
 * 
 * @return 如果存在返回 true
 */
const bool File::isExist() const {
	// 获取当前路径
	#if defined _WIN32 || defined _WIN64
		#ifdef _WIN32
			return _access(this->absolutePath.c_str(), 0) == 0;
		#else
			return access(this->absolutePath.c_str(), 0) == 0;
		#endif	
	#elif defined __APPLE__ || defined __MACH__
		struct stat s;
		return stat(this->absolutePath.c_str(), &s) == 0;
	#elif defined __linux__
		struct stat s;
		return stat(this->absolutePath.c_str(), &s) == 0;
	#elif defined __unix || defined __unix__
		struct stat s;
		return stat(this->absolutePath.c_str(), &s) == 0;
	#else
		#error Not support operator system!
	#endif
}

/**
 * @Author: CimZzz
 * @Date: 2023-03-07 15:12:05
 * 
 * 获取文件类型
 * 
 * @param 返回文件类型
 */
const FileType File::getFileType() const {
	if(!this->isExist()) {
		return FileType::NotExists;
	}

	// 获取当前路径
	#if defined _WIN32 || defined _WIN64
		WIN32_FIND_DATAA fileInfo;
		if (::FindFirstFileA(this->absolutePath.c_str(), &fileInfo) != INVALID_HANDLE_VALUE) {
			if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
				return FileType::Directory;
			}
			else {
				return FileType::File;
			}
		}
	#elif defined __APPLE__ || defined __MACH__
		struct stat stat;
		if(lstat(this->absolutePath.c_str(), &stat) == -1) {
			return FileType::Unknown;
		}
		if(S_ISDIR(stat.st_mode)) {
			return FileType::Directory;
		}
		if(S_ISREG(stat.st_mode)) {
			return FileType::File;
		}
	#elif defined __linux__
		struct stat stat;
		if(lstat(this->absolutePath.c_str(), &stat) == -1) {
			return FileType::Unknown;
		}
		if(S_ISDIR(stat.st_mode)) {
			return FileType::Directory;
		}
		if(S_ISREG(stat.st_mode)) {
			return FileType::File;
		}
	#elif defined __unix || defined __unix__
		struct stat stat;
		if(lstat(this->absolutePath.c_str(), &stat) == -1) {
			return FileType::Unknown;
		}
		if(S_ISDIR(stat.st_mode)) {
			return FileType::Directory;
		}
		if(S_ISREG(stat.st_mode)) {
			return FileType::File;
		}
	#else
		#error Not support operator system!
	#endif

	return FileType::Unknown;
}


/**
 * @Author: CimZzz
 * @Date: 2023-03-29 16:24:39
 * 
 * 判断文件是否为文件夹
 * 
 * @return 如果是返回true
 */
const bool File::isDir() const {
	return this->getFileType() == FileType::Directory;
}

/**
 * @Author: CimZzz
 * @Date: 2023-03-29 16:25:02
 * 
 * 判断文件是否为文件
 * 
 * @return 如果是返回true
 */
const bool File::isFile() const {
	return this->getFileType() == FileType::File;
}

/**
 * @Author: CimZzz
 * @Date: 2023-03-07 15:10:13
 * 
 * 创建文件夹
 * 
 * @return 如果成功创建文件夹返回 true
 */
const bool File::mkdir() const {
	if(this->isExist()) {
		return this->getFileType() == FileType::Directory;
	}
	#if defined _WIN32 || defined _WIN64
		return ::mkdir(this->absolutePath.c_str()) == 0;
	#elif defined __APPLE__ || defined __MACH__
		return ::mkdir(this->absolutePath.c_str(), 0775) == 0;
	#elif defined __linux__
		return ::mkdir(this->absolutePath.c_str(), 0775) == 0;
	#elif defined __unix || defined __unix__
		return ::mkdir(this->absolutePath.c_str(), 0775) == 0;
	#else
		#error Not support operator system!
	#endif
}

/**
 * @Author: CimZzz
 * @Date: 2023-03-08 15:34:19
 * 
 * 嵌套创建文件夹
 * 
 * @return 如果嵌套创建成功返回 true
 */
const bool File::mkdirs() const {
	auto const & parentFile = this->getParent();
	if(!parentFile.isExist() && !parentFile.mkdirs()) {
		return false;
	}
	return this->mkdir();
}

/**
 * @Author: CimZzz
 * @Date: 2023-03-07 15:33:11
 * 
 * 创建文件
 * 
 * @return 如果成功创建文件返回 true
 */
const bool File::touch() const {
	if(this->isExist()) {
		return this->getFileType() == FileType::File;
	}

	std::ofstream ostream;
	ostream.open(this->absolutePath);
	if(ostream.bad()) {
		return false;
	}
	ostream.close();
	return !ostream.fail();
}

/**
 * @Author: CimZzz
 * @Date: 2023-03-07 15:48:17
 * 
 * 删除文件或文件夹
 * 
 * @return 如果成功删除文件或文件夹, 返回 true
 */
const bool File::remove() const {
	if(!this->isExist()) {
		return true;
	}

	// 检查是否为根目录, 防止根目录删除
	// 获取当前路径
	if(_checkPathIsRoot(this->absolutePath)) {
		// 无法删除根目录
		return false;
	}
	
	// 先遍历删除全部子文件
	auto childFiles = this->listFiles();
	for(auto iter = childFiles->begin() ; iter != childFiles->end() ; iter ++) {
		if(!iter->remove()) {
			return false;
		}
	}

	return std::remove(this->absolutePath.c_str()) == 0;
}

/**
 * @Author: CimZzz
 * @Date: 2023-03-07 19:15:20
 * 
 * 重命名文件
 * 
 * @param file 重命名之后的文件
 * 
 * @return 如果重命名成功返回 true
 */
const bool File::rename(const File& file) const {
	return this->rename(&file);
}

/**
 * @Author: CimZzz
 * @Date: 2023-03-07 19:15:20
 * 
 * 重命名文件
 * 
 * @return 如果重命名成功返回 true
 */
const bool File::rename(const File* file) const {
	return std::rename(this->absolutePath.c_str(), file->absolutePath.c_str()) == 0;
}

/**
 * @Author: CimZzz
 * @Date: 2023-03-07 19:24:59
 * 
 * 复制文件
 * 
 * @param file 复制之后的文件
 * 
 * @return 如果复制成功返回 true
 */
const bool File::copy(const File& file) const {
	return this->copy(&file);
}

/**
 * @Author: CimZzz
 * @Date: 2023-03-07 19:24:59
 * 
 * 复制文件
 * 
 * @param file 复制之后的文件
 * 
 * @return 如果复制成功返回 true
 */
const bool File::copy(const File* file) const {
	if(this->isExist()) {
		std::ifstream input(this->absolutePath);
		if(!input) {
			return false;
		}

		std::ofstream output(file->absolutePath);
		if(!output) {
			return false;
		}

		output << input.rdbuf();
		
		input.close();
		output.close();

		return !output.fail();
	}
	return false;
}


/**
 * @Author: CimZzz
 * @Date: 2023-03-07 16:12:37
 * 
 * 返回文件夹下所有文件
 * 
 * @return 文件列表指针(非空)
 */
const std::shared_ptr<std::vector<File>> File::listFiles() const {
	auto vectorPtr = std::make_shared<std::vector<File>>();
	if(this->getFileType() == FileType::Directory) {
		#if defined _WIN32 || defined _WIN64
			intptr_t handle;
			_finddata_t findData;

			std::string dir = this->absolutePath + FILE_SEPERATOR + "*";
			handle = ::_findfirst(dir.c_str(), &findData);
			if (handle == -1) {
				return vectorPtr;
			}
			do {
				if (strcmp(findData.name, ".") == 0 || strcmp(findData.name, "..") == 0) {
					continue;
				}
				File file(findData.name);
				vectorPtr->push_back(file);
			} while (::_findnext(handle, &findData) == 0);
			::_findclose(handle);
		#elif defined __APPLE__ || defined __MACH__
			DIR *dir;
			struct dirent *diread;
			if((dir = opendir(this->absolutePath.c_str())) != nullptr) {
				while((diread = readdir(dir)) != nullptr) {
					if(strcmp(diread->d_name, ".") == 0 || strcmp(diread->d_name, "..") == 0) {
						continue;
					}
					File file;
					file.absolutePath = this->absolutePath;
					file.sepStack = std::stack<int>(this->sepStack);
					file._initFilePath(diread->d_name, true);
					vectorPtr->push_back(file);
				}
			}
		#elif defined __linux__
			DIR *dir;
			struct dirent *diread;
			if((dir = opendir(this->absolutePath.c_str())) != nullptr) {
				while((diread = readdir(dir)) != nullptr) {
					if(strcmp(diread->d_name, ".") == 0 || strcmp(diread->d_name, "..") == 0) {
						continue;
					}
					File file((const File *)this, diread->d_name);
					vectorPtr->push_back(file);
				}
			}
		#elif defined __unix || defined __unix__
			DIR *dir;
			struct dirent *diread;
			if((dir = opendir(this->absolutePath.c_str())) != nullptr) {
				while((diread = readdir(dir)) != nullptr) {
					if(strcmp(diread->d_name, ".") == 0 || strcmp(diread->d_name, "..") == 0) {
						continue;
					}
					File file((const File *)this, diread->d_name);
					vectorPtr->push_back(file);
				}
			}
		#else
			#error Not support operator system!
		#endif
	}

	return vectorPtr;
}

/**
 * @Author: CimZzz
 * @Date: 2023-03-07 18:48:37
 * 
 * 获取父目录
 * 
 * @return 父目录文件
 */
const File File::getParent() const & {
	File parentFile;
	parentFile.absolutePath = this->parentPath;
	parentFile.sepStack = std::stack<int>(this->sepStack);
	if(this->sepStack.size() > 1) {
		parentFile.sepStack.pop();
	}
	parentFile._initFileInfo();
	return parentFile;
}

/**
 * @Author: CimZzz
 * @Date: 2023-03-07 18:53:59
 * 
 * 判断是否为根目录
 * 
 * @return 如果是根目录返回 true
 */
const bool File::isRoot() const {
	return _checkPathIsRoot(this->absolutePath);
}

/**
 * @Author: CimZzz
 * @Date: 2022-02-17 17:00:41
 * 
 * 获取文件最后的修改时间
 * 
 * @return 修改时间
 */
int64_t File::getModifyTime() const {
	#if defined _WIN32 || defined _WIN64
		#error Not support operator system!
	#elif defined __APPLE__ || defined __MACH__
		struct stat result;
		if(stat(this->absolutePath.c_str(), &result) == 0) {
			return result.st_mtimespec.tv_sec * 1000 + result.st_mtimespec.tv_nsec / 1000;
		}
		return -1;
	#elif defined __linux__
		struct stat result;
		if(stat(this->absolutePath.c_str(), &result) == 0) {
			return result.st_mtim.tv_sec * 1000 + result.st_mtim.tv_nsec / 1000;
		}
		return -1;
	#elif defined __unix || defined __unix__
		struct stat result;
		if(stat(this->absolutePath.c_str(), &result) == 0) {
			return result.st_mtim.tv_sec * 1000 + result.st_mtim.tv_nsec / 1000;
		}
		return -1;
	#else
		#error Not support operator system!
	#endif
}

/**
 * @Author: CimZzz
 * @Date: 2023-03-08 16:49:12
 * 
 * 获取相对路径
 * 
 * @param relativeToFile 对比文件
 * 
 * @return 相对路径
 * 
 * @note 如果当前文件路径不包含对比文件的路径, 则会返回绝对路径
 */
const std::string File::relativeTo(const File* relativeToFile) const {
	auto const & filePath = relativeToFile->absolutePath;
	auto relativeLength = filePath.length();
	auto length = this->absolutePath.length();
	if(length < relativeLength) {
		return this->absolutePath;
	}

	char buffer[PATH_MAX];

	for(int i = 0 ; i < relativeLength ; i ++) {
		if(this->absolutePath.at(i) != filePath.at(i)) {
			return this->absolutePath;
		}
	}

	if(length == relativeLength) {
		return ".";
	}

	if(this->absolutePath.at(relativeLength) != FILE_SEPERATOR) {
		return this->absolutePath;
	}

	return this->absolutePath.substr(relativeToFile->sepStack.top() + 1);
}

/**
 * @Author: CimZzz
 * @Date: 2023-03-08 16:49:55
 * 
 * 获取相对路径
 * 
 * @param relativeToFile 对比文件
 * 
 * @return 相对路径
 * 
 * @note 如果当前文件路径不包含对比文件的路径, 则会返回绝对路径
 */
const std::string File::relativeTo(const File& relativeToFile) const {
	return this->relativeTo(&relativeToFile);
}

/**
 * @Author: CimZzz
 * @Date: 2023-03-08 14:36:36
 * 
 * 获取文件分割符
 * 
 * @return 文件分隔符
 */
const char File::getFileSeperator() {
	return FILE_SEPERATOR;
}

/**
 * @Author: CimZzz
 * @Date: 2023-02-28 18:08:47
 * 
 * 初始化路径信息
 * 
 * @param path 路径
 * @param isPartOfPath 明确路径是部分路径
 */
void File::_initFilePath(const std::string& path, bool isPartOfPath) {
	char pathBuffer[VL_MAX_PATH + 1];

	std::string fullPath = "";

	int endIndex = 0;
	bool lastIsSep = false;
	// 目录模式
	// 0 - 无
	// 1 - .
	// 2 - ..
	// 3 - 不匹配
	unsigned char dirMode = 0;

	if(isPartOfPath) {
		::memcpy(pathBuffer, this->absolutePath.c_str(), this->absolutePath.length());
		endIndex = this->absolutePath.length();
		fullPath = FILE_SEPERATOR + path;
	} else if(_checkPathBeginRoot(path)) {
		// 判断路径是否为绝对路径
		fullPath = path;
	} else {
		// 如果不是绝对路径, 用当前命令行绝对路径拼接上目标路径
		// 获取当前路径
		#if defined _WIN32 || defined _WIN64
			_getcwd(pathBuffer, VL_MAX_PATH);
			std::string prefix(pathBuffer);
		#elif defined __APPLE__ || defined __MACH__
			getcwd(pathBuffer, VL_MAX_PATH);
			std::string prefix(pathBuffer);
		#elif defined __linux__
			getcwd(pathBuffer, VL_MAX_PATH);
			std::string prefix(pathBuffer);
		#elif defined __unix || defined __unix__
			getcwd(pathBuffer, VL_MAX_PATH);
			std::string prefix(pathBuffer);
		#else
			#error Not support operator system!
		#endif

		fullPath = prefix + FILE_SEPERATOR + path;
	}

	const char * pathBytes = fullPath.c_str();
	auto byteLength = fullPath.length();

	for(int i = 0 ; i < byteLength ; i ++) {
		char c = pathBytes[i];
		if(c == '.') {
			lastIsSep = false;
			switch(dirMode) {
				case 0:
					dirMode = 1;
					continue;
				case 1:
					dirMode = 2;
					continue;
				case 2:
					dirMode = 3;
					pathBuffer[endIndex ++] = '.';
					pathBuffer[endIndex ++] = '.';
					break;
				default:
					break;
			}
			pathBuffer[endIndex ++] = '.';
		}
		else if(c == FILE_SEPERATOR) {
			if(lastIsSep) {
				continue;
			}
			switch(dirMode) {
				case 1:
					dirMode = 0;
					lastIsSep = true;
					continue;
				case 2:
					dirMode = 0;
					if(this->sepStack.empty()) {
						throw FileException { .errorCode = VL_FILE_ERROR_PATH };
					}
					if(this->sepStack.size() == 1) {
						endIndex = this->sepStack.top() + 1;
					} else {
						this->sepStack.pop();
						endIndex = this->sepStack.top() + 1;
					}
					lastIsSep = true;
					continue;
				default:
					dirMode = 0;
					break;
			}
			lastIsSep = true;
			this->sepStack.push(endIndex);
			pathBuffer[endIndex ++] = c;
		} else {
			lastIsSep = false;

			switch(dirMode) {
				case 1:
					dirMode = 3;
					pathBuffer[endIndex ++] = '.';
					break;
				case 2:
					dirMode = 3;
					pathBuffer[endIndex ++] = '.';
					pathBuffer[endIndex ++] = '.';
					break;
			}

			pathBuffer[endIndex ++] = c;
		}

	}

	if(dirMode == 2) {
		if(this->sepStack.empty()) {
			throw FileException { .errorCode = VL_FILE_ERROR_PATH };
		}
		if(this->sepStack.size() == 1) {
			endIndex = this->sepStack.top() + 1;
		} else {
			this->sepStack.pop();
			endIndex = this->sepStack.top() + 1;
		}
	}

	if(pathBuffer[endIndex - 1] == FILE_SEPERATOR && this->sepStack.top() == endIndex - 1 && this->sepStack.size() > 1) {
		this->sepStack.pop();
		endIndex --;
	}

	pathBuffer[endIndex] = '\0';
	this->absolutePath = pathBuffer;
	this->_initFileInfo();
}

/**
 * @Author: CimZzz
 * @Date: 2023-02-28 18:50:03
 * 
 * 初始化路径信息
 */
void File::_initFileInfo() {
	if(_checkPathIsRoot(this->absolutePath)) {
		this->parentPath = this->absolutePath;
		this->name = "";
		this->baseName = "";
		this->ext = ""; 
		return;
	}
	auto tempName = this->absolutePath.substr(this->sepStack.top() + 1);
	auto pointIdx = tempName.find_last_of('.');
	this->parentPath = this->absolutePath.substr(0, this->sepStack.top());
	if(this->parentPath.empty()) {
		this->parentPath = FILE_SEPERATOR;
	}
	if(pointIdx == -1) {
		this->baseName = tempName;
		this->ext = "";
	} else {
		this->baseName = tempName.substr(0, pointIdx);
		this->ext = tempName.substr(pointIdx + 1);
	}

	this->name = tempName;
}